package club.yuit.desktop.ui.producer;


import club.yuit.desktop.ProducerDataHandler;
import club.yuit.desktop.editor.LogEditorPane;
import club.yuit.desktop.layout.WrapLayout;
import club.yuit.desktop.model.ProducerContext;
import club.yuit.desktop.ui.KeyValuePropertyPanel;
import club.yuit.desktop.ui.UITools;
import club.yuit.rmq.service.ProducerService;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.message.MessageId;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static club.yuit.desktop.ui.UIConstants.DEFAULT_SIZE;

/**
 * @author: yuit
 * @date: 2023/11/28 23:25
 */
public class ProducerUI extends JFrame {

    private String topic;
    private ProducerService producerService;
    private ProducerContext context;

    private LogEditorPane logEditorPane;



    public ProducerUI(Component relativeTo, String topic, ProducerService producerService) {
        this.producerService = producerService;
        this.setVisible(true);
        this.setSize(DEFAULT_SIZE);
        this.topic = topic;
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel mainPanel = new JPanel();

        if (this.topic.startsWith("%SYS%")){
            this.topic = this.topic.substring(5);
        }

        ImageIcon icon = UITools.getSVGIcon("/imgs/Icon.svg",16,16);
        this.setIconImage(icon.getImage());

        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.add(logPanel());
        initContext();



        this.setTitle("Producer:[" + this.topic + "]");
        mainPanel.add(messageControlPanel());
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(relativeTo);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                // 窗口关闭后，关闭链接
                Producer producer = context.getProducer();
                if (producer!=null){
                    try {
                        producer.close();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });





    }

    /**
     * 初始化上下问参数，便于各个UI之间传递参数
     */
    private void initContext() {
        context = new ProducerContext();
        context.setProducerService(producerService);
        context.setTopic(topic);
        context.setProducer(producerService.normalProducer());
        context.setLogEditorPane(logEditorPane);
    }

    private JPanel messageControlPanel() {
       return new ProducerMessageControlPanel(context);
    }


    private JPanel logPanel() {
        JPanel panel = new JPanel();

        BorderLayout layout = new BorderLayout();
        panel.setLayout(layout);

        logEditorPane = new LogEditorPane();
        logEditorPane.setEditable(false);
        JScrollPane sc = new JScrollPane(logEditorPane);

        panel.add(sc, BorderLayout.CENTER);

        return panel;
    }







}







